package com.qc31.monitorlib.ui.singlemap

import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.amap.api.maps.AMap
import com.qc31.baselib.auto.AutoDisposeViewModel
import com.qc31.baselib.utils.CacheMMKV
import com.qc31.comlib.entity.GpsDetail
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.subjects.PublishSubject
import java.util.concurrent.TimeUnit

/**
 *@Author Jasper
 *@Time   2020/12/4
 *@Desc   单车监控
 */
class SingleMapViewModel : AutoDisposeViewModel() {
    var gpsDetail: GpsDetail? = null
    private var subscribe: Disposable? = null

    var isCarTraffic: Boolean
        get() = CacheMMKV.decodeBoolean("single_isTraffic")
        set(value) = CacheMMKV.encode("single_isTraffic", value)

    var isCarName: Boolean
        get() = CacheMMKV.decodeBoolean("single_isCarName")
        set(value) = CacheMMKV.encode("single_isCarName", value)

    var mMapType: Int
        get() = CacheMMKV.decodeInt("single_isMapNormal", AMap.MAP_TYPE_NORMAL)
        set(value) = CacheMMKV.encode("single_isMapNormal", value)

    val locationSub: PublishSubject<GpsDetail> = PublishSubject.create()
    var carId: String? = null
    val startLive = MutableLiveData<Boolean>()

    var time: Long = 10

    init {
        time = CacheMMKV.decodeLong("single_map_time", 10)
    }

    /**
     * 获取定位信息
     */
    private fun getLocation() {
        if (carId.isNullOrEmpty()) {
            toast(R.string.hint_toast_choose_car)
        } else {
            ServiceHelper.getRepository().getGpsRInfo("", carId!!)
                .autoDispose(this)
                .subscribe({
                    if (it.isNullOrEmpty()) {
                        toast(R.string.toast_no_location_other)
                        startLive.postValue(true)
                        subscribe?.dispose()
                    } else {

                        gpsDetail = it[0]
                        locationSub.onNext(gpsDetail!!)
                    }
                    toastLoading(false)
                }, error)
        }
    }

    /**
     * 获取定位信息
     */
    private fun getLocation2() {
        if (carId.isNullOrEmpty()) {
            toast(R.string.hint_toast_choose_car)
        } else {
            ServiceHelper.getRepository().getGpsRInfo("", carId!!)
                .autoDispose(this)
                .subscribe({
                    if (it.isNullOrEmpty()) {
                        toast(R.string.toast_no_location_other)
                        startLive.postValue(true)
                        subscribe?.dispose()
                    } else {
                        toast(R.string.toast_refresh_success)
                        gpsDetail = it[0]
                        locationSub.onNext(gpsDetail!!)
                    }
                    toastLoading(false)
                }, error)
        }
    }

    /**
     * 开始请求
     */
    fun startTime() {
        toastLoading(true)
        subscribe?.dispose()
        subscribe =
            Flowable.interval(0, time, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread())
                .autoDispose(this)
                .subscribe {
                    getLocation()
                }

    }

    fun refresh() {
        getLocation()
    }

    fun refresh2() {
        getLocation2()
    }

    /**
     * 停止请求
     */
    fun stopTime() {
        subscribe?.dispose()
    }

    fun saveRefreshTime(refreshTime: Long) {
        time = refreshTime
        CacheMMKV.encode("single_map_time", refreshTime)
    }
}
